﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Collections;
using Components;
using System.Data;
using MsDAL;

namespace ServiceHelper
{
    public class Sys0a00Manager : Sys0a00Base
    {
        Sys0a00DAL dal = new Sys0a00DAL();

        private static Sys0a00Manager _entity;
        /// <summary>
        /// 得到PersonManager类的实体对象
        /// </summary>
        public static Sys0a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sys0a00Manager();
                return _entity;
            }
        }

        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public string CheckLogin(string name, string pwd, string mac, bool isCut)
        {
            string msg = "";
            string mk = "";

            string loginKey = Context.GetSKey(mac);
            if (string.IsNullOrEmpty(loginKey)) return "E1|链接超时，请重试！";

            SecurityEncryption.key = loginKey;
            name = SecurityEncryption.Decrypt(name);
            pwd = SecurityEncryption.Decrypt(pwd);

            Sys0a00 user = new Sys0a00();
            user.Usercode = name;
            user.Userpwd = pwd;

            Hashtable tb = new Hashtable();
            tb["@usercode"] = name;
            tb["@userpwd"] = SecurityEncryption.StringToMD5Hash(pwd);

            DataTable oUser = null;
            try
            {
                oUser = CommonManager.Entity.ExecDBFun("getserveruserbaseinfo", tb);
            }
            catch (Exception e)
            {
                return string.Format("{0}|{1}", "E1", e.Message);
            }

            if (oUser == null || 0 == oUser.Rows.Count)
            {
                return "E2|用户帐号或者用户密码错误";
            }

            bool flg = Context.AddUser(user, ContextUser.Convert(oUser), isCut, ref msg);
            mk = flg ? "S1" : "E1";

            if (flg)
            {
                Context.AddPs(msg, loginKey);//添加关系
            }

            //移除验证票
            Context.RemoveLoginKey(mac);

            return string.Format("{0}|{1}", mk, msg);
        }

        /// <summary>
        /// 编辑页面回写的方法
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="action"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public string SaveData(Hashtable tb, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();
            Hashtable hs = null;

            Sys0a00 model = new Sys0a00();
            if (tb != null && tb.Contains("comcode"))
            {
                tb["comcode"] = Validation.ToString(tb["comcode"]).Replace(".", "");
            }

            CommonUtil.GetInstance.CopyProperties(tb, model);

            if ("A" == action)//新增
            {
                model.Userpwd = SecurityEncryption.StringToMD5Hash(model.Userpwd);

                model.Muser = usercode;
                model.Mdate = DateTime.Now;
                lst.Add(this.Add(model));

                string rolecode = Validation.ToString(tb["rolecode"]);
                if (!string.IsNullOrEmpty(rolecode)) //用户角色不为空时，增加用户角色对应信息
                {
                    Sys0a02 sys0a02 = new Sys0a02();
                    sys0a02.Usercode = model.Usercode;
                    sys0a02.Rolecode = rolecode;

                    lst.Add(Sys0a02Manager.Entity.Add(sys0a02));
                }
            }
            else if ("M" == action)//修改
            {
                hs = new Hashtable();
                hs["usercode"] = model.Usercode;
                hs["userpwd"] = model.Userpwd;
                if (!CommonManager.Entity.HasData("Sys0a00", hs)) //检测密码是否改变
                {
                    model.Userpwd = SecurityEncryption.StringToMD5Hash(model.Userpwd);
                }
                model.Muser = usercode;
                model.Mdate = DateTime.Now;
                lst.Add(this.Update(model));
            }
            else if ("D" == action)//删除
            {
                //删除用户名角色表
                lst.Add(Sys0a02Manager.Entity.DeleteByUsercode(model.Usercode));

                //删掉权限表中的数据
                lst.Add(Sys0a05Manager.Entity.DeleteByUsercode(model.Usercode));

                lst.Add(Sys0a00Manager.Entity.Delete(model));
            }

            return CommonManager.Entity.SaveData(lst);
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="action"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public string UpdatePwd(Hashtable tb, string action, string usercode)
        {
            if (null == tb) return "[新密码]不能为空！";
            string pwd = Validation.ToString(tb["pwd"]);
            if (string.IsNullOrEmpty(pwd)) return "[新密码]不能为空！";

            string oldpwd = Validation.ToString(tb["oldpwd"]); //原密码

            bool mk = dal.UpdatePwd(usercode, pwd, oldpwd);
            if (!mk)
            {
                return "数据过期，原密码不正确！";
            }
            else
            {
                LogHelper.Entity.Out(string.Format("用户{0}密码修改成功!", usercode));
                return "";
            }
        }

        public SqlApp RemoveEmpno(string empno)
        {
            return dal.Remove(empno);
        }
    }
}
